package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api("员工管理模块")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        // 登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * @param employeePageQueryDTO: return Result<PageResult>
     * @author fenglai
     * @date 2023/12/6 10:50
     * @description 分页查询员工的功能
     */
    @GetMapping("/page")
    @ApiOperation("分页查询员工的功能")
    public Result<PageResult> pageSelectEmployee(EmployeePageQueryDTO employeePageQueryDTO) {

        log.info("正在执行分页查询员工的功能..................查询条件为:姓名:{},页码:{},每页记录数:{}", employeePageQueryDTO.getName(), employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        PageResult p = employeeService.pageSelectEmployee(employeePageQueryDTO);

        return Result.success(p);

    }

    /**
     * @param employeeDTO: return Result
     * @author fenglai
     * @date 2023/12/6 11:47
     * @description 先添员工的功能
     */

    @ApiOperation("新添员工的功能")
    @PostMapping
    public Result insertEmployee(@RequestBody EmployeeDTO employeeDTO) {


        log.info("正在执行新添员工的功能...............员工信息为:{}", employeeDTO);

        employeeService.insertEmployee(employeeDTO);

        return Result.success();
    }

    /**
     * @param id: return Result<Employee>
     * @author fenglai
     * @date 2023/12/7 19:41
     * @description 根据id查询员工的功能
     */

    @ApiOperation("根据id查询员工的功能")
    @GetMapping("/{id}")
    public Result<Employee> selectEmployeeById(@PathVariable Long id) {

        log.info("正在执行根据id查询员工的功能..........员工id为:{}", id);

        Employee employee = employeeService.selectEmployeeById(id);


        return Result.success(employee);
    }

    /**
     * @param employeeDTO: return Result
     * @author fenglai
     * @date 2023/12/7 19:42
     * @description 编辑员工的功能
     */
    @ApiOperation("编辑员工的功能")
    @PutMapping
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO) {

        log.info("正在执行编辑员工的功能............员工编辑信息为:{}", employeeDTO);

        employeeService.updateEmployee(employeeDTO);

        return Result.success();

    }

    /**
     * @param status:
     * @param id:     return Result
     * @author fenglai
     * @date 2023/12/10 16:23
     * @description 启用、禁用员工账号功能
     */
    @ApiOperation("启用、禁用员工账号")
    @PostMapping("/status/{status}")
    public Result updateEmployeeStatus(@PathVariable Integer status, Long id) {

        log.info("正在执行启用、禁用员工账号的功能..........状态:{}", status);

        employeeService.updateEmployeeStatus(status, id);

        return Result.success();

    }

/**
 * @param passwordEditDTO:
 * return Result
 * @author fenglai
 * @date 2023/12/10 16:47
 * @description 员工修改密码的功能
 */

    @ApiOperation("员工修改密码的功能")
    @PutMapping("/editPassword")
    public Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO) {

        log.info("正在执行员工修改密码的功能...............修改信息为:员工id{},原密码{},旧密码{}",
                passwordEditDTO.getEmpId(), passwordEditDTO.getOldPassword(), passwordEditDTO.getNewPassword());

        employeeService.updatePassword(passwordEditDTO);

        return Result.success();


    }

}
